
package fileby.gui.magicbox;

import fileby.core.ActionKey;
import fileby.core.IDisplayableFile;
import fileby.ctrl.sidebar.ISideBarItemListenerProvider;
import fileby.ctrl.sidebar.SideBarItemListener;
import fileby.ctrl.actions.IActionProvider;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author Ludwig
 */
public class MagicBoxItem extends javax.swing.JPanel {

    private IDisplayableFile file;
    private IActionProvider actionProvider;
    private ISideBarItemListenerProvider listenerProvider;
    private JPopupMenu popup;

    public MagicBoxItem(IDisplayableFile file,
            IActionProvider actionProvider,
            ISideBarItemListenerProvider listenerProvider) {

        this.file = file;
        this.listenerProvider = listenerProvider;
        this.actionProvider = actionProvider;
        initComponents();
        initCtrl();     
    }

    private void initCtrl() {
        initJPopupMenu();
        SideBarItemListener l = listenerProvider.create();
        popup.addPopupMenuListener(l);
        addMouseListener(l);
    }

    private void initJPopupMenu() {
        popup = new JPopupMenu();
        Action removeFromMagicBox = actionProvider.getControl(ActionKey.REMOVE_FROM_MAGIC_BOX);
        removeFromMagicBox.putValue(ActionKey.REMOVE_FROM_MAGIC_BOX.toString(), file.getAbsolutePath());
        popup.add(new JMenuItem(removeFromMagicBox));
        setComponentPopupMenu(popup);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        name = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));
        setMaximumSize(new java.awt.Dimension(32767, 25));
        setPreferredSize(new java.awt.Dimension(151, 25));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        setName(file.getAbsolutePath());
        name.setText(file.getName());
        setToolTipText(file.getAbsolutePath());
    }// </editor-fold>

    private javax.swing.JLabel name; 

}
